//
//  WSQPickerTextField.m
//  RACTest
//
//  Created by 翁胜琼 on 2018/10/15.
//  Copyright © 2018 翁胜琼. All rights reserved.
//

#import "WSQPickerTextField.h"
#import "WSQPickerTextFieldItem.h"
#import "WSQPickerTextFieldDropDownTextField.h"
#import <Masonry/Masonry.h>
#import <IQKeyboardManager/IQKeyboardManager.h>


@interface WSQPickerTextField()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    NSArray<WSQPickerTextFieldItem *> *_itemsArray0;
    NSArray<NSArray<WSQPickerTextFieldItem *> *> *_itemsArray1;
    NSArray<NSArray<WSQPickerTextFieldItem *> *> *_itemsArray2;
    NSArray<NSArray<WSQPickerTextFieldItem *> *> *_itemsArray3;
    NSArray<NSArray<WSQPickerTextFieldItem *> *> *_itemsArray4;
    
    NSInteger _numberOfComponents;
}

@property (nonatomic,strong) UIPickerView *pickerView;
@property (weak,nonatomic) UITextField *textField;
@property (strong,nonatomic) WSQPickerTextFieldDropDownTextField *dropdownTextField;

@end
@implementation WSQPickerTextField

@synthesize selectedRows = _selectedRows;

- (instancetype)initWithFrame:(CGRect)frame preferredStyle:(WSQPickerTextFieldStyle)style
{
    if (self = [super initWithFrame:frame])
    {
        _style = style;
        [self setupUI];
    }
    
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setupUI];
}

- (void)setItems:(NSArray *)items inComponent:(NSInteger)component
{
    if (component > 4)
    {
        return ;
    }
    
    if (component + 1 > _numberOfComponents)
    {
        _numberOfComponents = component + 1;
    }
    
    if (component == 0)
    {
        _itemsArray0 = [items copy];
    }else if(component == 1)
    {
        _itemsArray1 = [items copy];
    }else if(component == 2)
    {
        _itemsArray2 = [items copy];
    }else if(component == 3)
    {
        _itemsArray3 = [items copy];
    }else if(component == 4)
    {
        _itemsArray4 = [items copy];
    }
    
    [self reloadAllComponents];
}

- (NSInteger)numberOfComponents
{
    return [self.pickerView numberOfComponents];
}

- (NSInteger)numberOfRowsInComponent:(NSInteger)component
{
    return [self.pickerView numberOfRowsInComponent:component];
}

- (void)reloadComponent:(NSInteger)component
{
    [self.pickerView reloadComponent:component];
}

- (void)reloadAllComponents
{
    [self.pickerView reloadAllComponents];
}

// 设置 iqkeyboardmanager
- (void)setKeyboardManager
{
    [self.textField addDoneOnKeyboardWithTarget:self action:@selector(keyboardDoneBtnAction)];
}

- (void)keyboardDoneBtnAction
{
    [self setTextIncomponent:_numberOfComponents - 1];
    [[UIApplication sharedApplication].windows.firstObject.rootViewController.view endEditing:true];
}

// 更新视图
- (void)updatePickerTextField
{
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    
    [self setupUI];
}

// MARK: - setupUI
- (void)setupUI
{
    [self setupTextField];
    [self setupPickerView];
    [self setKeyboardManager];
}

- (void)setupTextField
{
    if (_style == WSQPickerTextFieldStyleDropDown)
    {
        [self setupDropdownTextField];
    }else if (_style == WSQPickerTextFieldStyleDefault)
    {
        [self setupDefaultTextField];
    }
}

- (void)setupDefaultTextField
{
    UITextField *textfield = [[UITextField alloc] init];
    [self addSubview:textfield];
    self.textField = textfield;
    
    [textfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self);
    }];
}

- (void)setupDropdownTextField
{
    self.dropdownTextField = [[NSBundle mainBundle]loadNibNamed:@"WSQPickerTextFieldDropDownTextField" owner:nil options:nil].firstObject;
    [self addSubview:self.dropdownTextField];
    [self.dropdownTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self);
    }];
}

- (void)setupPickerView
{
    self.pickerView = [[UIPickerView alloc] init];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    self.pickerView.backgroundColor = [UIColor whiteColor];
    self.textField.inputView = self.pickerView;
}

// MARK: - pickerDelegate&&DateSource
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component < _numberOfComponents - 1)
    {
        for (NSInteger i = component + 1; i <=_numberOfComponents - 1; i++) {
            [_pickerView selectRow:0 inComponent:i animated:true];
        }
    }
    
    // 设置 textfield 文字
    [self setTextIncomponent:component];
    
    // 设置被选中的 text
    NSMutableArray *selectedMutableRowsArray = [NSMutableArray arrayWithArray:self.selectedRows];
    [selectedMutableRowsArray setObject:[NSNumber numberWithInteger:row] atIndexedSubscript:component];
    self.selectedRows = [selectedMutableRowsArray copy];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    WSQPickerTextFieldItem *itemInRow;
    if (component == 0)
    {
        itemInRow = [self getSelectedItemsInComponent:component][row];
    }else
    {
        NSInteger selectedRowInComponent = [_pickerView selectedRowInComponent:component];
        itemInRow = [self getSelectedItemsInComponent:component][selectedRowInComponent][row];
    }
    
    return itemInRow.title;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return _numberOfComponents;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSArray *items = [self getSelectedItemsInComponent:component];
    return items.count;
}

- (NSArray *)getSelectedItemsInComponent:(NSInteger)component
{
    NSArray *items;
    if (component == 0)
    {
        items = _itemsArray0;
    }else if(component == 1)
    {
        items = _itemsArray1;
    }else if(component == 2)
    {
        items = _itemsArray2;
    }else if(component == 3)
    {
        items = _itemsArray3;
    }else if(component == 4)
    {
        items = _itemsArray4;
    }
    
    return items;
}

// 选中行更改的时候设置更改 text
- (void)setTextIncomponent:(NSInteger)component
{
    NSMutableString *text = [NSMutableString string];
    for (int i = 0; i<= _numberOfComponents - 1; i++) {
        WSQPickerTextFieldItem *itemInRow;
        if (i == 0)
        {
            NSInteger selectedRowInComponent = [_pickerView selectedRowInComponent:0];
            itemInRow = [self getSelectedItemsInComponent:i][selectedRowInComponent];
        }else
        {
            NSInteger selectedRowInComponent = [_pickerView selectedRowInComponent:i];
            itemInRow = [self getSelectedItemsInComponent:i][selectedRowInComponent][selectedRowInComponent];
        }
        
        [text appendString:itemInRow.title];
    }
    self.text = [text copy];
    self.textField.text = text;
}

// MARK: - setter && getter
- (void)setStyle:(WSQPickerTextFieldStyle)style
{
    _style = style;
    [self updatePickerTextField];
}

- (UITextField *)textField
{
    if (_style == WSQPickerTextFieldStyleDropDown)
    {
        return self.dropdownTextField.textfield;
    }
    return _textField;
}

- (NSString *)text
{
    return self.textField.text;
}

- (NSArray *)selectedRows
{
    if (!_selectedRows)
    {
        NSMutableArray *array = [NSMutableArray array];
        for (int i = 0; i<= _numberOfComponents - 1; i++)
        {
            [array addObject:@0];
        }
        return [array copy];
    }
    return _selectedRows;
}

- (void)setSelectedRows:(NSArray *)selectedRows
{
    _selectedRows = selectedRows;
    
    __weak typeof(self) weakSelf = self;
    [selectedRows enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        __strong typeof(weakSelf) self = weakSelf;
        //        [self.pickerView selectRow:[obj integerValue]inComponent:idx animated:true];
        [self setTextIncomponent:idx];
    }];
}

- (NSString *)placeholder
{
    return self.textField.placeholder;
}

- (void)setPlaceholder:(NSString *)placeholder
{
    self.textField.placeholder = placeholder;
}

- (void)setBorderColor:(UIColor *)borderColor
{
    self.layer.borderColor = borderColor.CGColor;
}

- (void)setBorderWidth:(CGFloat)borderWidth
{
    self.layer.borderWidth = borderWidth;
}

- (void)setCornerRadius:(CGFloat)cornerRadius
{
    self.layer.cornerRadius = cornerRadius;
    [self clipsToBounds];
}

- (void)setFontSize:(CGFloat)fontSize
{
    _fontSize = fontSize;
    self.textField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:fontSize];
}

- (void)setText:(NSString *)text
{
    _textField.text = text;
}

@end
